package com.centling.smartSealForPhone.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

public class ActivityCountManager {
    private static List<Activity> activityList = new ArrayList<>();

    public static void addActivity(Activity activity) {
        activityList.add(activity);
    }

    public static Activity currentActivity() {
        if (activityList.isEmpty()) {
            return null;
        }
        return activityList.get(activityList.size() - 1);
    }

    public static void removeActivity(Activity activity) {
        activityList.remove(activity);
    }

    public static void finishActivity(Activity activity) {
        if (activity != null) {
            activityList.remove(activity);
            activity.finish();
        }
    }

    public int getActivityCount() {
        return activityList.size();
    }

    public void clear() {
        activityList.clear();
    }

    public static void finishActivity(Class<?> cls) {
        for (Activity activity : activityList) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
                break;
            }
        }
    }

    public static Activity findActivity(Class<?> cls) {
        for (Activity activity : activityList) {
            if (activity.getClass().equals(cls)) {
                return activity;
            }
        }
        return null;
    }

    public static void finishAll() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        activityList.clear();
    }
}